package com.ftg.learnmybatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用来完成对数据库homework里面的student表的crud
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/3/10 下午1:28
 */
public class StudentMain {

    /**
     * xmlbase
     * Mapped Statements collection  =====> sqlsession(MappedStatement) --- collection  ---- sql
     * @param args
     */
    public static void main(String[] args) {
        //工程绝对路径
        String mybatisconfig = "mybatis-config.xml";
        try {
            //io java - txt （读取*.xml里面的信息到java里面）
            Reader reader = Resources.getResourceAsReader(mybatisconfig);

            // 生成SqlSessionFactory  根据xml参数，生成一个生成SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            //通过工厂生成一个缓存
            SqlSession session = sqlSessionFactory.openSession();
            //使用缓存里面封装jdbc操作，执行  student.sel 这条 sql语句
//            Map<String,Object> map = new HashMap<>(2);
//            map.put("sname","张");
//            map.put("sno","s002");
//            List<Map<String,Object>> list = session.selectList("student.sel2",map);
//            Student stu = new Student();
//            stu.setSno("s001");
//            stu.setSname("张");
//            List<Map<String,Object>> list = session.selectList("student.sel3",stu);
            Student stu = new Student();
            stu.setSno("s001");
            stu.setSname("张");
            List<Student> list = session.selectList("student.sel4",stu);

            System.out.println(list);
            //关闭缓存
            session.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
